package com.ruicar.afs.cloud.contract.gateway.dto;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.enums.SystemType;
import lombok.Data;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * The type PageInfo
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.07.20 21:17:40
 * @since 1.0
 */
@Data
public class PageInfo{
    /**
     * Page size
     * <p>Description: </p>
     */
    private Long pageSize;
    /**
     * Page index
     * <p>Description: </p>
     */
    private Long pageIndex;
    /**
     * Records
     * <p>Description: </p>
     */
    @JSONField(serializeUsing = RecordsSerializer.class)
    private JSONArray records;
    /**
     * Total
     * <p>Description: </p>
     */
    private Long total;
    /**
     * Condition
     * <p>Description: </p>
     */
    private Map<String,Object> condition;
    /**
     * Total info
     * <p>Description: </p>
     */
    @JSONField(serializeUsing = TotalMapSerializer.class)
    private Map<SystemType,Long> totalInfo = new HashMap<>();

    /**
     * The type TotalMapSerializer
     * <p>Description: </p>
     *
     * @author Fzero
     * @version 1.0
     * @date Created on 2020.07.20 21:17:40
     * @since 1.0
     */
    public static class TotalMapSerializer implements ObjectSerializer{
        /**
         * Write *
         *
         * @param serializer serializer
         * @param object     object
         * @param fieldName  field name
         * @param fieldType  field type
         * @param features   features
         * @throws IOException io exception
         */
        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
            SerializeWriter out = serializer.out;
            Map<String,Long> stringMap  =new HashMap<>();
            if(object!=null){
                Map<SystemType,Long> objects = (Map<SystemType,Long>)object;
                objects.entrySet().forEach(entry->{
                    stringMap.put(AfsEnumUtil.key(entry.getKey()),entry.getValue());
                });
                out.write(JSON.toJSONString(stringMap));
            }else{
                out.write("{}");
            }
        }
    }

    public static class RecordsSerializer implements ObjectSerializer{
        /**
         * Write *
         *
         * @param serializer serializer
         * @param object     object
         * @param fieldName  field name
         * @param fieldType  field type
         * @param features   features
         * @throws IOException io exception
         */
        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
            SerializeWriter out = serializer.out;
            if(object!=null){
                out.write(JSONUtil.toJsonStr(object));
            }else{
                out.write("[]");
            }
        }
    }
}

